package com.eascs.admin.o2o.enterpriseZX.service;

import java.util.GregorianCalendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eascs.admin.common.utils.StringUtils;
import com.eascs.admin.o2o.back.vo.O2oBackPageVo;
import com.eascs.admin.o2o.enterpriseZX.dao.O2oEnterpriseZxDao;
import com.eascs.admin.o2o.enterpriseZX.entity.O2oEnterpriseZxEntity;
import com.eascs.admin.o2o.enterpriseZX.vo.O2oEnterpriseZxVo;
import com.eascs.admin.o2o.internal.util.BaseUtils;
import com.eascs.admin.o2o.o2ozx.controller.O2oZxController;
import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.DateUtils;
import com.eascs.common.util.StringUtil;
import com.eascs.web.sso.UserHolder;

@Service
public class O2oEnterpriseZxService {

	@Autowired
	O2oEnterpriseZxDao o2oEnterpriseZxDao;
	
	
	private final Logger logger = LoggerFactory.getLogger(O2oEnterpriseZxService.class);
	
	/**
	 * @Description:
	 *               <p>
	 *               分页查询代偿分期记录
	 *               </p>
	 */
	public PageResult<Object[]> getByPage(O2oBackPageVo vo){
		return o2oEnterpriseZxDao.getByPage(vo);
	}
	
	
	public int isnert(O2oEnterpriseZxEntity o2oEnterpriseZxEntity){
		return o2oEnterpriseZxDao.insert(o2oEnterpriseZxEntity);
	}
	
	public int update(O2oEnterpriseZxEntity o2oEnterpriseZxEntity){
		return o2oEnterpriseZxDao.updateById(o2oEnterpriseZxEntity);
	}
	
	public O2oEnterpriseZxEntity getEntityById(String id){
		return o2oEnterpriseZxDao.getById(O2oEnterpriseZxEntity.class, id);
	}
	public O2oEnterpriseZxEntity getEntityByloanapply(String loanapplyNo){
		String sql="SELECT * from o2o_enterprise_zx  WHERE loanapply_no=? ";
		return  o2oEnterpriseZxDao.findEntity(sql, O2oEnterpriseZxEntity.class, loanapplyNo);
	}
	/**
	 *检查身份证是否有企业征信流程在走
	 * 
	 * @param id
	 * @return
	 */
	public int checkO2oQYzx(String idCard) {
		String sql = "select count(*) from o2o_enterprise_zx where customer_card = ? and zx_status !='CLO' and is_del='N'" ;
		int i = o2oEnterpriseZxDao.findInteger(sql, idCard);
		return i;
	}
	
	public O2oEnterpriseZxEntity getEntityByLoanNo(String loanApllyNo){
		String sql = "select * from o2o_compensatory_back where loan_apply_no = ? ";
		return o2oEnterpriseZxDao.findEntity(sql, O2oEnterpriseZxEntity.class, loanApllyNo);
	}
	
	public List<O2oEnterpriseZxEntity> getEntityByLoanApplyNo(String loanapplyNo){
		return o2oEnterpriseZxDao.getEntityByLoanApplyNo(loanapplyNo);
	}
	
	public int getCountByLoanApplyNo(String loanapplyNo){
		return o2oEnterpriseZxDao.getCountByLoanApplyNo(loanapplyNo);
	}
	
	public int getCountPlan(String mainId){
		return o2oEnterpriseZxDao.getCountPlan(mainId);
	}
	
	public PageResult<Object[]> getO2oEnterpriseZxEntitys(String loanapplyNo){
		return o2oEnterpriseZxDao.getO2oEnterpriseZxEntitys(loanapplyNo);
	}
	
	
	/**
	 * 企业征信分页查询
	 * @param O2oEnterpriseZxVo
	 * @return
	 */
	public PageResult<Object[]> findByPage(O2oEnterpriseZxVo o2oZxVo) {
		SqlPageQuery<Object[]> pageQuery = new SqlPageQuery<Object[]>(Object[].class,o2oZxVo.getPageSize() ,
				o2oZxVo.getPageIndex() == null ? BaseUtils.PAGE_INDEX : o2oZxVo.getPageIndex());
		StringBuffer sql = new StringBuffer();
		sql.append(" select zx.id,zx.bill_code,zx.customer_name,zx.zx_yyzz_name,zx.zx_bank,zx.operator_name,"
				+ "zx.ltd_name,zx.create_date,zx.zx_status ");
		sql.append(" FROM o2o_enterprise_zx zx ");
		sql.append(" where  ");
		sql.append(" zx.is_del !='Y'  "); //(作废，和正常的都显示)
		
		sql.append(" and (zx.create_id="+UserHolder.getUser().getNo());
		if (!StringUtils.isEmpty(o2oZxVo.getType())) {
			sql.append(" or zx.operator_id in ("+o2oZxVo.getType()+"))");
		}else {
			sql.append(" or /**DATA_PERMISSION(zx.LTD)**/ 1=1 ) ");
		}
		
		if (StringUtil.hasText(o2oZxVo.getBillCode())) {
			sql.append(" and zx.bill_code like ?");
			pageQuery.addLikeParam(o2oZxVo.getBillCode());
		}
		
		if (StringUtil.hasText(o2oZxVo.getOperatorName())) {
			sql.append(" and zx.operator_name like ?");
			pageQuery.addLikeParam(o2oZxVo.getOperatorName());
		}
		if (StringUtil.hasText(o2oZxVo.getUserFullName())) {
			sql.append(" and zx.customer_name like ?");
			pageQuery.addLikeParam(o2oZxVo.getUserFullName());
		}
		if (StringUtil.hasText(o2oZxVo.getZxBank())) {
			sql.append(" and zx.zx_bank = ?");
			pageQuery.addParam(o2oZxVo.getZxBank());
		}
		if (StringUtil.hasText(o2oZxVo.getZxStatus())) {
			sql.append(" and zx.zx_status = ?");
			pageQuery.addParam(o2oZxVo.getZxStatus());
		}
		if (StringUtil.hasText(o2oZxVo.getLtdName())) {
			sql.append(" and zx.ltd_name like ?");
			pageQuery.addLikeParam(o2oZxVo.getLtdName());
		}
		if (StringUtil.hasText(o2oZxVo.getZxYyzzName())) {
			sql.append(" and zx.zx_yyzz_name like ?");
			pageQuery.addLikeParam(o2oZxVo.getZxYyzzName());
		}
		
		if (StringUtil.hasText(o2oZxVo.getCreateStartTime())) {
			sql.append(" and zx.create_date >= ? ");
			pageQuery.addParam(o2oZxVo.getCreateStartTime());
		}
		if (StringUtil.hasText(o2oZxVo.getCreateEndTime())) {
			sql.append(" and zx.create_date <= ? ");
			GregorianCalendar gc = new GregorianCalendar();
			gc.setTime(DateUtils.formateStandDate(o2oZxVo.getCreateEndTime()));
			gc.add(5, 1);
			pageQuery.addParam(gc.getTime());
		}
		
		if (StringUtil.hasText(o2oZxVo.getZxStartTime())) {
			sql.append(" and zx.zx_time >= ? ");
			pageQuery.addParam(o2oZxVo.getZxStartTime());
		}
		if (StringUtil.hasText(o2oZxVo.getZxEndTime())) {
			sql.append(" and zx.zx_time <= ? ");
			GregorianCalendar gc = new GregorianCalendar();
			gc.setTime(DateUtils.formateStandDate(o2oZxVo.getZxEndTime()));
			gc.add(5, 1);
			pageQuery.addParam(gc.getTime());
		}
		
		sql.append(" order by zx.create_date	desc ");
		//pageQuery.setSumColumnIndexs(new int[]{10});
		pageQuery.setQuerySql(sql.toString());
		logger.info("findByPage,sql={}", sql.toString());
		return o2oEnterpriseZxDao.findDataWithPage(pageQuery);
	}
	
	public PageResult<Object[]> queryFqZxDTbg(String loanapplyNo){
		return o2oEnterpriseZxDao.queryFqZxDTbg(loanapplyNo);
	}
	public PageResult<Object[]> loanFqZxbgnum(String IdNum){
		return o2oEnterpriseZxDao.loanFqZxbgnum(IdNum);
	}
	
	/**
	 * @Title: bcIsSelectedByCustomerType
	 * @Description: 一个自然月内在同一家银行是否查询过企业征信报告
	 * tyshxydm 统一社会编码
	 */
	public boolean isMothSelectedQYZX(String tyshxydm,String bank){
		boolean flag=false;
		String sql=" select count(*) from o2o_enterprise_zx oez "
				+ " where oez.zx_status='CLO' and oez.zx_time is not null and date_format(oez.zx_time,'%Y-%m') = date_format(NOW(),'%Y-%m') "
				+ "and oez.is_del = 'N' and oez.zx_bank <> 'BC'  and oez.zx_tyshxydm=? and oez.zx_bank = ? " ;
		Integer num=o2oEnterpriseZxDao.findInteger(sql, tyshxydm,bank);
		if(num>0){
			flag=true;
		}
		return flag;
	}
	
}
